package com.itestor.iptv.modules.common.actions;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.itestor.iptv.global.GlobalConstant;
import com.itestor.iptv.global.utils.MenuPermissionAdapter;
import com.itestor.iptv.modules.common.domain.SystemLog;
import com.itestor.iptv.modules.common.service.SystemLogService;
import com.itestor.iptv.modules.common.service.UserService;
import com.itestor.iptv.modules.common.utils.CommonConstant;
import com.itestor.iptv.modules.common.vo.User;

/**
 * 登陆Action.
 * @author Mr.li
 */
public class LoginAction extends Action {

    /**
     * Logger for this class.
     */
    private static final Logger logger = Logger.getLogger(LoginAction.class);

    /**
     * 用户账号业务对象.
     */
    private UserService userService;

    /**
     * 
     */
    private SystemLogService systemLogService;

    /**
     * 
     */
    private static final String LOGIN_LOG = "登录系统";

    /**
     * @param mapping action mapping
     * @param form action form
     * @param request http request
     * @param response http response
     * @return action forward
     * @throws Exception any exception
     */
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        logger.info("User logging in. - Username: " + username);
        User user = userService.login(username, password);
        if (user == null) {
            logger.info("User login failed! - Username: " + username);
        } else {
            logger.info("User Name: " + user.getUsername());
            request.getSession().setAttribute(CommonConstant.SESSION_ATTR_USER,
                    user);

            SystemLog log = new SystemLog();
            log.setClientIp(request.getRemoteAddr());
            log.setLogDesc(LOGIN_LOG);
            log.setLogTime(new Date());
            log.setOperator(user.getUsername());
            log.setLogType(CommonConstant.SYSTEM_LOG_LONGIN);
            
            systemLogService.create(log);            
            
            afterSuccessLogin(request, response);
        }

        return mapping.findForward(GlobalConstant.FORWARD_SUCCESS);
    }

    /**
     * 登陆成功之后的业务逻辑过程.
     * @param request http request
     * @param response http response
     * @throws Exception any exeption
     */
    protected void afterSuccessLogin(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        initDynamicMenu(request);
    }

    /**
     * 初始化动态菜单.
     * @param request http request
     * @throws Exception http respones
     */
    private void initDynamicMenu(HttpServletRequest request) throws Exception {
        User user = (User) request.getSession().getAttribute(
                CommonConstant.SESSION_ATTR_USER);
        MenuPermissionAdapter adapter = new MenuPermissionAdapter(user);
        request.getSession().setAttribute(
                CommonConstant.SESSION_ATTR_MENU_ADAPTER, adapter);
    }

    /**
     * @param pUserService the userService to set
     */
    public void setUserService(UserService pUserService) {
        this.userService = pUserService;
    }

    /**
     * @param pSystemLogService
     */
    public void setSystemLogService(SystemLogService pSystemLogService) {
        systemLogService = pSystemLogService;
    }

}
